package cn.snailhd.utils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class ObjUtils {
	
	/**
	 * 获取对象属性值
	 * @param field
	 * @param obj
	 * @return
	 */
	public static Object getValue(String field, Object obj) {
		if(obj instanceof Map ){
			return ((Map) obj).get(field);
		}
		
		Class userCla = (Class) obj.getClass();
		try {
			Field f = userCla.getDeclaredField(field);
			f.setAccessible(true); // 设置些属性是可以访问的
			Object val = new Object();
			return f.get(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		Map map = new HashMap();
		map.put("index", 1);
		Object re = getValue("index", map);
		System.out.println(re);
	}
}
